const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: '',
    origin_ps:'',//原始密码
    new_ps:'',//新密码
    conform_ps:'',// 密码确认
    imodifyPs:false,
    loadModal:false//显示加载框
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
  },

  init: function () {
    var that = this;
    this.showload(true);
    db.collection('es_website').where({
      wdbm: wx.getStorageSync('account')
    }).get().then(res => {
      console.log(res)
      that.showload(false);
      var result = res.data;
      if (result.length > 0) {
        this.setData({
          userinfo: res.data[0]
        })
      }

    }).catch(err => {
      console.log(err)
      that.showload(false)
      wx.showToast({
        title: '获取数据失败',
      })
    })
  },
  modifyps: function (e) {
    this.setData({
      imodifyPs: e.detail.value,
      origin_ps:'',
      new_ps:'',
      conform_ps:''
    })
  },
  inputOriginPs:function(e){
    this.setData({
      origin_ps:e.detail.value
    })
  },
  inputOriginPs:function(e){
    this.setData({
      origin_ps:e.detail.value
    })
  },
  inputNewPs:function(e){
    this.setData({
      new_ps:e.detail.value
    })
  },
  inputConform:function(e){
    this.setData({
      conform_ps:e.detail.value
    })
  },
  modifyUserPs: function() {
    var that = this;
    if (this.data.origin_ps == '') {
      wx.showToast({
        title: '请输原密码',
      })
      return;
    }
    if (this.data.new_ps == '') {
      wx.showToast({
        title: '请输入新密码',
      })
      return;
    }
    if(this.data.new_ps != this.data.conform_ps) {
      wx.showToast({
        title: '两次密码不一致',
      })
      return;
    }
    if(this.data.new_ps == this.data.origin_ps) {
      wx.showToast({
        title: '与原密码一致',
      })
      return;
    }
    this.showload(true);
    //进行网络请求修改
    wx.cloud.callFunction({
      name:'modifyps',
      data:{
        websitecode:wx.getStorageSync('account'),
        origin_ps:this.data.origin_ps,
        new_ps:this.data.new_ps,
        conform_ps:this.data.conform_ps
      }
    }).then(res=>{
      console.log(res);
      that.showload(false)
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: res.result.msg
      })
      that.setData({
        origin_ps:'',
        new_ps:'',
        conform_ps:''
      })
    }).catch(err=>{
      that.showload(false)
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '修改密码失败'
      })
    })
  },
  showload:function(stats){
    this.setData({
      loadModal:stats
    })
  }
})